<template>
  <AmBaseTable ref="AmBaseTableRef"
               :schema="modelSchema"
               :record-config="record"
               @paramsChange="getList"
               :operations="[
                  {
                    label: '导出',
                    auth: ['am:amBorrow:export'],
                    buttonProps: {
                      type: 'warning',
                      icon: 'el-icon-download',
                    }
                  },
                ]"
               :actions="[
                  {
                    label: '审批',
                    auth: ['am:amBorrow:approval'],
                    disabledCtrl: (row) => row.status !== '1',
                    clickCB: this.handleApprove,
                    buttonProps: {
                      icon: 'el-icon-edit-outline',
                    }
                  },
                  {
                    label: '借出',
                    auth: ['am:amBorrow:borrow'],
                    tooltip: (row) => {
                      if(row.billType === 'borrow') {
                        return '借阅无需进行此操作'
                      } else if(['0', '1'].includes(row.status)) {
                        return '请先审核信息后，再进行借出操作'
                      } else if(row.status === '4') {
                        return '档案已借出'
                      } else if(row.status === '5') {
                        return '流程已关闭'
                      } else {
                        return false
                      }
                    },
                    disabledCtrl: (row) => (row.billType === 'borrow') || (['0', '1', '4', '5'].includes(row.status)),
                    clickCB: this.handleBorrow,
                    buttonProps: {
                      icon: 'el-icon-upload2',
                    }
                  },
                  {
                    label: '归还',
                    auth: ['am:amBorrow:return'],
                    tooltip: (row) => {
                      if(row.billType === 'borrow') {
                        return '借阅无需进行此操作'
                      } else if(['0', '1'].includes(row.status)) {
                        return '请先审核信息后，再进行借出操作'
                      } else if(row.status === '3') {
                        return '档案未借出'
                      }  else if(row.status === '5') {
                        return '流程已关闭'
                      } else {
                        return false
                      }
                    },
                    disabledCtrl: (row) => (row.billType === 'borrow') || (['0', '1', '3', '5'].includes(row.status)),
                    clickCB: this.handleReturn,
                    buttonProps: {
                      icon: 'el-icon-download',
                    }
                  },
                ]">
    <!--    <template v-slot:column-id="scope">{{ scope }}</template>-->
    <template v-slot:column-billName="scope">
      <el-tag>{{ scope.row.billName }}</el-tag>
    </template>
    <template v-slot:column-statusName="scope">
      <el-tag :type="statusTypeMap[scope.row.status]">{{ scope.row.statusName }}</el-tag>
    </template>
    <!-- 扩展区 -->
    <template #extend>
      <ModalApprove ref="ModalApproveRef" @success="getList" />
      <ModalBorrow ref="ModalBorrowRef" @success="getList" />
      <ModalReturn ref="ModalReturnRef" @success="getList" />
    </template>
  </AmBaseTable>
</template>

<script>
import AmBaseTable from "../../../components/AmComp/AmBaseTable.vue";
import {modelSchema} from "./model";
import ModalApprove from "./ModalApprove.vue";
import ModalBorrow from "./ModalBorrow.vue";
import ModalReturn from "./ModalReturn.vue";
import {listAmBorrow} from "@/api/am/amBorrow";

export default {
  name: "Description",
  components: {
    AmBaseTable,
    ModalApprove,
    ModalBorrow,
    ModalReturn,
  },
  data() {
    return {
      modelSchema,
      record: {
        // 显示搜索条件
        showSearch: true,
        data: [],
        total: 0,
        loading: false,
      },
      statusTypeMap: {
        '0': 'warning',
        '1': 'default',
        '2': 'success',
        '3': 'danger',
        '5': 'info',
      }
    };
  },
  mounted() {
    this.$nextTick(() => this.getList());
  },
  methods: {
    /** 查询档案著录列表 */
    getList() {
      this.record.loading = true;
      const queryParams = this.$refs.AmBaseTableRef.getQueryParams();
      listAmBorrow(queryParams).then(response => {
        this.record.data = response.rows;
        this.record.total = response.total;
        this.record.loading = false;
      });
    },
    /** 审批按钮操作 **/
    handleApprove(row) {
      this.$refs.ModalApproveRef.open(row);
    },
    /** 借出按钮操作 **/
    handleBorrow(row) {
      this.$refs.ModalBorrowRef.open(row);
    },
    /** 归还按钮操作 **/
    handleReturn(row) {
      this.$refs.ModalReturnRef.open(row);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('am/description/export', {
        ...this.queryParams
      }, `description_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
